package cn.com.lyj6851.modules.user.controller;

import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.user.domain.YdySystemUserTask;
import cn.com.lyj6851.modules.user.service.YdySystemUserTaskService;
import cn.com.lyj6851.modules.user.service.dto.YdySystemUserTaskQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "商城:用户任务管理")
@RestController
@RequestMapping("api")
public class SystemUserTaskController {

    private final YdySystemUserTaskService ydySystemUserTaskService;

    public SystemUserTaskController(YdySystemUserTaskService ydySystemUserTaskService) {
        this.ydySystemUserTaskService = ydySystemUserTaskService;
    }

    @AopLog("查询")
    @ApiOperation(value = "查询")
    @GetMapping(value = "/ydySystemUserTask")
    @PreAuthorize("hasAnyRole('admin','SYSTEMUSERTASK_ALL','SYSTEMUSERTASK_SELECT')")
    public ResponseEntity getYdySystemUserTasks(YdySystemUserTaskQueryCriteria criteria,
                                                Pageable pageable){
        Sort sort = Sort.by(Sort.Direction.ASC,"level_id");//new Sort(Sort.Direction.ASC, "level_id");
        Pageable pageableT = PageRequest.of(pageable.getPageNumber(),
                pageable.getPageSize(),
                sort);
        return new ResponseEntity(ydySystemUserTaskService.queryAll(criteria,pageableT),
                HttpStatus.OK);
    }

    @AopLog("新增")
    @ApiOperation(value = "新增")
    @PostMapping(value = "/ydySystemUserTask")
    @PreAuthorize("hasAnyRole('admin','SYSTEMUSERTASK_ALL','SYSTEMUSERTASK_CREATE')")
    public ResponseEntity create(@Validated @RequestBody YdySystemUserTask resources){
        return new ResponseEntity(ydySystemUserTaskService.save(resources),HttpStatus.CREATED);
    }

    @AopLog("修改")
    @ApiOperation(value = "修改")
    @PutMapping(value = "/ydySystemUserTask")
    @PreAuthorize("hasAnyRole('admin','SYSTEMUSERTASK_ALL','SYSTEMUSERTASK_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YdySystemUserTask resources){
        //if(StrUtil.isNotEmpty("22")) throw new BadRequestException("演示环境禁止操作");
        ydySystemUserTaskService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @AopLog("删除")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/ydySystemUserTask/{id}")
    @PreAuthorize("hasAnyRole('admin','SYSTEMUSERTASK_ALL','SYSTEMUSERTASK_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        //if(StrUtil.isNotEmpty("22")) throw new BadRequestException("演示环境禁止操作");
        ydySystemUserTaskService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
